package com.feinno.urcs.group.fxdb;

import com.feinno.database.ConnectionString;
import com.feinno.database.Database;
import com.feinno.database.spi.DatabaseManager;
import org.helium.framework.BeanContext;
import org.helium.framework.BeanIdentity;
import org.helium.framework.configuration.ConfigProvider;
import org.helium.framework.configuration.FieldLoader;
import org.helium.framework.entitys.SetterNode;

import java.io.IOException;

/**
 * Created by honghao on 7/13/15.
 */
public class HADatabaseLoader implements FieldLoader {
	@Override
	public Object loadField(SetterNode node) {
		String dbName = node.getInnerText();
		BeanIdentity id = BeanIdentity.parseFrom(ConfigProvider.BEAN_ID);
		ConfigProvider configProvider = (ConfigProvider) BeanContext.getContextService().getBean(id).getBean();
		String connStr = configProvider.loadText(dbName + ".properties");

		Database db = null;
		try {
			db = DatabaseManager.INSTANCE.getDatabase(dbName, ConnectionString.fromText(connStr));
		} catch (IOException ex) {
			throw new IllegalArgumentException("Bad connection strings for:" + dbName, ex);
		}
		return db;
	}
}
